package com.huike.business.service.impl;


import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.domain.vo.BusinessTrackVo;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.business.mapper.TbBusinessTrackRecordMapper;
import com.huike.business.service.ITbBusinessTrackRecordService;
import com.huike.common.utils.SecurityUtils;
import com.huike.common.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 商机跟进记录Service业务层处理
 *
 * @author wgl
 * @date 2021-04-28
 */
@Transactional //事务
@Service
public class TbBusinessTrackRecordServiceImpl implements ITbBusinessTrackRecordService {
    //自动注入mapper
    @Autowired
    private TbBusinessTrackRecordMapper tbBusinessTrackRecordMapper;
    @Autowired
    private TbBusinessMapper tbBusinessMapper;

    /**
     * m
     *
     * @param businessTrackVo 商机跟进记录
     */
    @Override
    public void addbusinessTrackVo(BusinessTrackVo businessTrackVo) {
        //1. 转换线索跟进的数据, 页面数据---数据库数据
        TbBusinessTrackRecord tbBusinessTrackRecord = new TbBusinessTrackRecord();
        //copy数据,tbBusinessTrackRecord是数据库数据,   businessTrackVo是页面数据
        BeanUtils.copyBeanProp(tbBusinessTrackRecord,businessTrackVo);
        //传入用户名,从数据库中传
        tbBusinessTrackRecord.setCreateBy(SecurityUtils.getUsername());
        //添加数据到mapper
        tbBusinessTrackRecordMapper.add(tbBusinessTrackRecord);

        //2. 修改线索的状态
        TbBusiness tbBusiness = new TbBusiness();
        //copy数据,tbBusiness是数据库数据,   businessTrackVo是页面数据
        BeanUtils.copyBeanProp(tbBusiness, businessTrackVo);
        //获取此线索的id
        tbBusiness.setId(tbBusinessTrackRecord.getBusinessId());
        //获取此线索的状态
        tbBusiness.setStatus(TbBusiness.StatusType.FOLLOWING.getValue());

        tbBusinessMapper.updateTbBusiness(tbBusiness);
    }

    @Override
    public List<TbBusinessTrackRecord> findbussinessByid(Long businessId) {
        List<TbBusinessTrackRecord> list = tbBusinessTrackRecordMapper.findbussinessByid(businessId);
        return list;
    }


}
